﻿using System;
using UnityEngine;
using UnityEngine.UIElements;

public class BaseRayDetect {
	readonly Transform _entityTransform;
	Vector2 _offset1;
	Vector2 _offset2;
	protected readonly float _distance;
	protected readonly string _layerName;
	protected readonly Func<bool, bool, bool> _condition;

	public virtual bool IsDetect(Vector2 direction)
		=> _condition(Hit(_offset1, direction), Hit(_offset2, direction));

	public BaseRayDetect(Vector2 offset1, Vector2 offset2, Transform transform, float d, string layerName, Func<bool, bool, bool> condition) {
		_offset1 = offset1;
		_offset2 = offset2;
		_entityTransform = transform;
		_distance = d;
		_layerName = layerName;
		_condition = condition;
	}

	protected virtual bool Hit(Vector2 offset, Vector2 dir)
		=> Physics2D.Raycast(new Vector2(_entityTransform.position.x, _entityTransform.position.y) + offset,
			dir, _distance, 1 << LayerMask.NameToLayer(_layerName)).collider != null;

}